{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# encoding: utf-8\n",
    "\n",
    "import pandas as pd\n",
    "import tushare as ts\n",
    "import datetime\n",
    "import json\n",
    "from pymongo import MongoClient\n",
    "\n",
    "pd.set_option('display.width', None)  # 设置字符显示宽度\n",
    "pd.set_option('display.max_rows', None)  # 设置显示最大行\n",
    "pd.set_option('display.max_columns', None)  # 设置显示最大行\n",
    "\n",
    "if __name__ == '__main__':\n",
    "\n",
    "    # 连接数据库\n",
    "    client = MongoClient('localhost', 27017)\n",
    "    db = client.futures\n",
    "    marketdata = db.marketdata\n",
    "\n",
    "    # 填写日期\n",
    "    begin = datetime.date(2018, 9, 21)\n",
    "    end = datetime.date(2018, 11, 2)\n",
    "    for i in range((end - begin).days + 1):\n",
    "        day = begin + datetime.timedelta(days=i)\n",
    "        days = day.strftime('%Y-%m-%d')\n",
    "        # 四大交易所行情\n",
    "        dce = ts.get_dce_daily(days)\n",
    "        shf = ts.get_shfe_daily(days)\n",
    "        zce = ts.get_czce_daily(days)\n",
    "        cff = ts.get_cffex_daily(days)\n",
    "        frames = [dce, shf, zce, cff]\n",
    "        try:\n",
    "            # 合并四大交易所行情表\n",
    "            df2 = pd.concat(frames)\n",
    "\n",
    "            # 计算品种指数收盘价\n",
    "            df2 = df2.dropna(axis=0, how='any')\n",
    "#             # df2 = df2.convert_objects(convert_numeric=True)\n",
    "            df2 = df2.apply(pd.to_numeric, errors=\"ignore\")\n",
    "#             df2['closev'] = df2['close'] * df2['volume']\n",
    "#             df2['openv'] = df2['open'] * df2['volume']\n",
    "#             df2 = df2.groupby('variety')['volume', 'closev','openv'].sum()\n",
    "#             df2['set_close'] = round(df2['closev'] / df2['volume'])\n",
    "#             df2['set_open'] = round(df2['openv'] / df2['volume'])\n",
    "#             df2['change'] = df2['set_close'] - df2['set_open']\n",
    "#             df2['date'] = days\n",
    "#             df2 = df2.dropna(axis=0, how='any')\n",
    "#             df2 = df2.reset_index()\n",
    "#             df2 = df2[['date', 'variety', 'set_close','set_open','change']]\n",
    "            print(df2)\n",
    "            print(marketdata.insert(json.loads(df2.T.to_json()).values()))\n",
    "#             print(json.loads(df2.T.to_json()).values())\n",
    "        except:\n",
    "            print(days,'数据异常')\n",
    "            continue"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
